package cn.xfdzx.android.apps.shop.presenter;

import android.util.Log;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ClassShopBeen;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.MoreGoodsBean;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopGoodListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.bean.StoreInfoBeen;
import cn.xfdzx.android.apps.shop.bean.XFDGoodsDetailsBeen;
import cn.xfdzx.android.apps.shop.contract.ClassShopContract;
import cn.xfdzx.android.apps.shop.contract.ShopRetailContract;
import cn.xfdzx.android.apps.shop.model.ClassShopModel;
import cn.xfdzx.android.apps.shop.model.ShopRetailModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class ShopRetailPresenter extends BasePresenter<ShopRetailContract.View> implements ShopRetailContract.Presenter {

    private ShopRetailContract.Model model;

    public ShopRetailPresenter() {
        model = new ShopRetailModel();
    }

    @Override
    public void initData(String good_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initData(good_id) .compose(RxScheduler.<XFDGoodsDetailsBeen>Flo_io_main())
                .as(mView.<XFDGoodsDetailsBeen>bindAutoDispose())
                .subscribe(new Consumer<XFDGoodsDetailsBeen>() {
                    @Override
                    public void accept(XFDGoodsDetailsBeen bean) throws Exception {
                        mView.onInitDataSuccess(bean);
                        mView.hideLoading();

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void confirm(String  cartId,String  user_curent_lng,String  user_curent_lat) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.confirm(cartId,user_curent_lng,user_curent_lat)
                .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onConfirmSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }


    @Override
    public void shopRetail(String shopId) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.shopRetail(shopId).compose(RxScheduler.<ShopGoodListBeen>Flo_io_main())
                .as(mView.<ShopGoodListBeen>bindAutoDispose()).subscribe(new Consumer<ShopGoodListBeen>() {
            @Override
            public void accept(ShopGoodListBeen been) throws Exception {
                mView.onShopRetailSuccess(been);
//                mView.hideLoading();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
//                mView.hideLoading();
            }
        });
    }

    @Override
    public void shopStoreInfo(String shopId, String sales_model) {
        if (!isViewAttached()) {
            return;
        }
        model.shopStoreInfo( shopId,sales_model)
                .compose(RxScheduler.<StoreInfoBeen>Flo_io_main())
                .as(mView.<StoreInfoBeen>bindAutoDispose())
                .subscribe(new Consumer<StoreInfoBeen>() {
                    @Override
                    public void accept(StoreInfoBeen bean) throws Exception {
                        mView.onShopStoreInfoSuccess(bean);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);

                    }
                });
    }


    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.addCar(is_new,uniqueId,productId,cartNum) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void shopCarList(String store_id, String index_status) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.shopCarList(store_id,index_status)
                .compose(RxScheduler.<ShopCarListBeen>Flo_io_main())
                .as(mView.<ShopCarListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopCarListBeen>() {
                    @Override
                    public void accept(ShopCarListBeen bean) throws Exception {
                        mView.onShopCarListSuccess(bean);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void updateCarNum(String id,int number,int one_position,int position) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.updateCarNum(id,number+"")
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onUpdateCarNumSuccess(bean,number,one_position,position);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }
    @Override
    public void removeGood(String ids,String good_id,int position,boolean isAll) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.removeGood(ids)
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onRemoveGoodSuccess(bean,good_id,position,isAll);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getStoreGoodsList(Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getStoreGoodsList(map)
                .compose(RxScheduler.<MoreGoodsBean>Flo_io_main())
                .as(mView.<MoreGoodsBean>bindAutoDispose())
                .subscribe(new Consumer<MoreGoodsBean>() {
                    @Override
                    public void accept(MoreGoodsBean bean) throws Exception {
                        mView.getStoreGoodsListSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getGoodsCollection(Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getGoodsCollection(map)
                .compose(RxScheduler.<MoreGoodsBean>Flo_io_main())
                .as(mView.<MoreGoodsBean>bindAutoDispose())
                .subscribe(new Consumer<MoreGoodsBean>() {
                    @Override
                    public void accept(MoreGoodsBean bean) throws Exception {
                        mView.getGoodsCollectionSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getGoodsCancleCollection(Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getGoodsCancleCollection(map)
                .compose(RxScheduler.<MoreGoodsBean>Flo_io_main())
                .as(mView.<MoreGoodsBean>bindAutoDispose())
                .subscribe(new Consumer<MoreGoodsBean>() {
                    @Override
                    public void accept(MoreGoodsBean bean) throws Exception {
                        mView.getGoodsCancleCollectionSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getFocusShop(String storeid) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getFocusShop(storeid)
                .compose(RxScheduler.<StoreInfoBeen>Flo_io_main())
                .as(mView.<StoreInfoBeen>bindAutoDispose())
                .subscribe(new Consumer<StoreInfoBeen>() {
                    @Override
                    public void accept(StoreInfoBeen bean) throws Exception {
                        mView.getFocusShopSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getShopRemakeRetail(Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
//        if (isLoading){
//            mView.showLoading();
//        }
        model.getShopRemakeRetail(map)
                .compose(RxScheduler.<ShopRemakeListRetailBean>Flo_io_main())
                .as(mView.<ShopRemakeListRetailBean>bindAutoDispose())
                .subscribe(new Consumer<ShopRemakeListRetailBean>() {
                    @Override
                    public void accept(ShopRemakeListRetailBean bean) throws Exception {
                        mView.onGetShopRemakeRetailSuccess(bean);
                        mView.hideLoading();


                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();


                    }
                });
    }

}
